import { PageData } from "@/interface";
import request from "@/request";
import { User } from "../user/service";

// 重要时间结点
export interface ptimeNode {
  time?: string; //创建时间
  info?: string;
  operator?: User; //操作人
  state?: string; //  "0" 正常  "1"  已删除
  deleteTime?: string; //删除时间
  deleteOperator?: User; // 删除的人
}

export interface ProjectBean {
  id?: string;
  name: string; //项目名称
  number?: string; //项目编号
  describe?: string; //描述
  notes?: string; //备注
  startTime: string; //开始时间
  endTime: string; //计划结束时间
  users: User[]; //当前关联的人员
  timeNode?: ptimeNode[]; //关键节点
}

const projectService = {
  upDateProject: (project: ProjectBean) => {
    const e: any = { ...project };
    if (project.users) {
      e.users = JSON.stringify(project.users);
    }
    if (project.timeNode) {
      e.timeNode = JSON.stringify(project.timeNode);
    }
    return request.put<any>("/api/project", e);
  },

  addProject: (project: ProjectBean) => {
    const e: any = { ...project };
    if (project.users) {
      e.users = JSON.stringify(project.users);
    }
    if (project.timeNode) {
      e.timeNode = JSON.stringify(project.timeNode);
    }
    return request.post<any>("/api/project", e);
  },

  getProjectByPage: async (
    { current, pageSize }: { current: number; pageSize: number },
    formData?: any
  ) => {
    return request.get<PageData<ProjectBean>>("/api/project/page", {
      params: {
        page: current - 1,
        size: pageSize,
        ...formData,
      },
    });
  },

  getAllUser: () => {
    return request.get<User[]>("/api/user/list");
  },

  deleteProject: (id: string) => {
    return request.delete(`/api/project/${id}`);
  },
  findPriject: (id: string) => {
    return request.get(`/api/project/${id}`);
  },
  addTimeNode: (data: any) => {
    return request.post(`/api/project/addTimeNode`, data);
  },
  deleteTimeNode: (data: any) => {
    return request.post(`/api/project/deleteTimeNode`, data);
  },
  findPrijectPlan: (data: any) => {
    return request.get(`/api/project/getPlanPage`, { params: data });
  },
  dataExport: (data: any) => {
    return request.get(`/api/project/getPlanDoc`, { params: data });
    // return request.post(`/api/project/getPlanDoc`, data);
  },
};

export default projectService;
